package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomizeException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 按条件分页查询套餐
     * @param curPage
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> querySetMealByPage(Integer curPage, Integer pageSize, String name) {
        // 设置分页对象
        Page<SetmealDto> page = new Page<>(curPage, pageSize);
        return setmealMapper.querySetMealByPage(page,name);
    }

    /**
     * 根据套餐id查询套餐和套餐下的菜品
     * @param id
     * @return
     */
    @Override
    public SetmealDto querySetMealById(Long id) {
        return setmealMapper.querySetMealById(id);
    }

    /**
     * 添加套餐
     * @param setmealDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSetMeal(SetmealDto setmealDto) {
        // 添加套餐
        setmealMapper.insert(setmealDto);
        // 套餐菜品设置套餐id,添加套餐中的菜品
        List<SetmealDish> setMealDishes = setmealDto.getSetmealDishes();
        if (!setMealDishes.isEmpty()) {
            setMealDishes.forEach(setMealDish -> setMealDish.setSetmealId(setmealDto.getId()));
            setmealDishService.saveBatch(setMealDishes);
        }
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSetMeal(SetmealDto setmealDto) {
        // 更新套餐
        setmealMapper.updateById(setmealDto);
        // 删除套餐下的菜品
        setmealDishService.lambdaUpdate().eq(SetmealDish::getSetmealId, setmealDto.getId()).remove();
        // 新增套餐对应的菜品
        List<SetmealDish> setMealDishes = setmealDto.getSetmealDishes();
        if (!setMealDishes.isEmpty()) {
            setMealDishes.forEach(setMealDish -> setMealDish.setSetmealId(setmealDto.getId()));
            setmealDishService.saveBatch(setMealDishes);
        }
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteSetMeal(List<Long> ids) {
        // 套餐关联购物车、订单详情
        Integer countShoppingCart = shoppingCartService.lambdaQuery()
                                    .in(ShoppingCart::getSetmealId, ids).count();
        if (countShoppingCart > 0) {
            throw new CustomizeException("套餐存在于购物车中，请先清除购物车");
        }
        Integer countOrderDetail = orderDetailService.lambdaQuery()
                                    .in(OrderDetail::getSetmealId, ids).count();
        if (countOrderDetail > 0) {
            throw new CustomizeException("套餐存在于订单详情中，请先清除订单");
        }
        // 套餐状态查询
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId).eq(Setmeal::getStatus, 1);
        Integer countSetMealStatus = setmealMapper.selectCount(lqw);
        if (countSetMealStatus > 0) {
            throw new CustomizeException("有套餐正在出售，请先停售");
        }
        // 删除套餐关联的菜品
        setmealDishService.lambdaUpdate().in(SetmealDish::getSetmealId, ids).remove();
        // 删除套餐
        setmealMapper.deleteBatchIds(ids);
    }
}
